package com.example.alibabavodossservice.vod.video;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.vod.model.v20170321.*;
import org.springframework.beans.BeanUtils;

import java.util.List;

import static com.example.alibabavodossservice.vod.InitVODConstants.VOD_CLIENT;


/**
 * desc: 处理音视频指令集
 *
 * @author xugenyin
 */
public class Cmd {

    /**
     * 获取音视频上传凭证相应
     *
     * @param videoParameter 主要请求参数
     * @return CreateUploadVideoResponse 获取音视频上传地址和凭证响应
     * @throws ClientException ClientException
     */
    public static CreateUploadVideoResponse createUploadVideoRequest(VideoParameter videoParameter) throws ClientException {
        //1.检查必要参数
        VideoParameter.checkMustParam(videoParameter);
        //2.获取音视频上传凭证响应
        CreateUploadVideoRequest uploadVideoRequest = new CreateUploadVideoRequest();
        uploadVideoRequest.setFileName(videoParameter.getFileName());
        uploadVideoRequest.setTitle(videoParameter.getTitle());
        uploadVideoRequest.setDescription(videoParameter.getDescription());
        uploadVideoRequest.setTags(videoParameter.getTags());
        uploadVideoRequest.setCateId(videoParameter.getCateId());
        uploadVideoRequest.setCoverURL(videoParameter.getCoverURL());
        return VOD_CLIENT.getAcsResponse(uploadVideoRequest);
    }


    /**
     * 刷新上传音频凭证
     * <p
     * 如果视频上传凭证失效（默认有效期为3000秒），请调用刷新视频上传凭证接口重新获取上传凭证。
     *
     * @param VideoId VideoId 需要被刷新的音 视频ID
     * @return 刷新上传视频凭证请求响应
     * @throws ClientException 连接异常
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(String VideoId) throws ClientException {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setAcceptFormat(FormatType.JSON);
        request.setVideoId(VideoId);
        //设置请求超时时间
        request.setSysReadTimeout(1000);
        request.setSysConnectTimeout(1000);

        return VOD_CLIENT.getAcsResponse(request);
    }


    /**
     * 搜索音视频列表
     *
     * @return Total,RequestId,VideoList
     * @throws ClientException 连接异常
     */
    public static GetVideoListResponse getVideoList(SearchVideoListParameter searchVideoListParameter) throws ClientException {
        GetVideoListRequest getVideoListRequest = new GetVideoListRequest();
        BeanUtils.copyProperties(searchVideoListParameter, getVideoListRequest);
        return VOD_CLIENT.getAcsResponse(getVideoListRequest);
    }

    /**
     * 通过videoId获取音视频信息
     *
     * @param videoId 视音频ID
     * @return GetVideoInfoResponse 音视频信息
     */
    public static GetVideoInfoResponse getVideInfoByVideoId(String videoId) throws ClientException {
        GetVideoInfoRequest getVideoInfoRequest = new GetVideoInfoRequest();
        getVideoInfoRequest.setVideoId(videoId);
        return VOD_CLIENT.getAcsResponse(getVideoInfoRequest);
    }

    /**
     * 通过videoIds获取音视频信息
     *
     * @param videoIds 视音频ID
     *                 音/视频ID列表。多个ID之间使用半角逗号（,）分隔，最多支持20个
     * @return GetVideoInfoResponse 音视频信息
     */
    public static GetVideoInfosResponse getVideListByVideoIds(String videoIds) throws ClientException {
        GetVideoInfosRequest getVideoInfosRequest = new GetVideoInfosRequest();
        getVideoInfosRequest.setVideoIds(videoIds);
        return VOD_CLIENT.getAcsResponse(getVideoInfosRequest);
    }

    /**
     * 通过videoId获取音视频信息 /视频详细信息。
     *
     * @param videoId 音/视频ID列表。多个ID之间使用半角逗号（,）分隔，最多支持20个
     * @return VideoBase 音/视频基本信息+PlayInfoList音/视频播放信息（流信息）。
     * 音/视频基本信息。
     * @throws Exception
     */
    public static GetPlayInfoResponse GetPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest();
        getPlayInfoRequest.setVideoId(videoId);
        return VOD_CLIENT.getAcsResponse(getPlayInfoRequest);
    }

    /**
     * 通过videoId获取音视频信息
     *
     * @param videos 视频ID 逗号分隔的string
     * @return List<GetVideoInfosResponse.Video> List<GetVideoInfosResponse.Video>
     * @throws ClientException
     */
    public static List<GetVideoInfosResponse.Video> bathGetVideoInfo(String videos) throws ClientException {
        GetVideoInfosRequest getVideoInfosRequest = new GetVideoInfosRequest();
        getVideoInfosRequest.setVideoIds(videos);
        return VOD_CLIENT.getAcsResponse(getVideoInfosRequest).getVideoList();
    }

    /**
     * 修改单个媒体信息
     *
     * @param videoParameter 媒体信息
     * @return UpdateVideoInfoResponse 修改单个媒体信息响应数据
     * @throws ClientException
     */
    public static UpdateVideoInfoResponse updateVideoInfo(VideoParameter videoParameter) throws ClientException {
        UpdateVideoInfoRequest updateVideoInfoRequest = new UpdateVideoInfoRequest();
        BeanUtils.copyProperties(videoParameter, updateVideoInfoRequest);
        return VOD_CLIENT.getAcsResponse(updateVideoInfoRequest);
    }

    /**
     * 批量修改媒体信息
     * 批量修改视频信息
     * 通过音/视频ID来定位视频，支持修改音/视频的标题、标签、描述等信息，传入参数则更新相应字段，否则该字段不会被覆盖或更新。
     * 一次最多支持修改20个音/视频的信息。
     *
     * @param videoParameterList 媒体信息列表
     * @return UpdateVideoInfosResponse 批量修改媒体信息响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateVideoInfosResponse bathUpdateVideoInfo(List<VideoParameter> videoParameterList) throws ClientException {
        UpdateVideoInfosRequest updateVideoInfosRequest = new UpdateVideoInfosRequest();
        updateVideoInfosRequest.setUpdateContent(JSON.toJSONString(videoParameterList));
        return VOD_CLIENT.getAcsResponse(updateVideoInfosRequest);
    }

    /**
     * 删除音视频源文件
     * 点播的媒体处理操作（转码、截图、审核等）均是对源文件的操作，因此源文件一旦删除，后续的媒体处理操作就无法进行
     * @param videoIds  音/视频ID列表。一次最多支持20个音/视频ID，多个ID之间用半角逗号（,）
     * @return DeleteVideoResponse 删除音视频源文件响应数据
     * @throws ClientException 连接异常
     */
    public static DeleteVideoResponse deleteVideo(String videoIds) throws ClientException {
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        deleteVideoRequest.setVideoIds(videoIds);
        return VOD_CLIENT.getAcsResponse(deleteVideoRequest);
    }

    /**
     * 删除媒体流
     * @param videoId 音视频
     * @param jobIds 工作流id，经过的媒体操作，例如转码，水印，截图等 多个ID之间使用半角逗号（,）分隔，最多支持同一个视频下的20个作业ID。
     * @return DeleteStreamResponse 删除媒体流响应数据
     * @throws ClientException 连接异常
     */
    public static DeleteStreamResponse deleteStream(String videoId,String jobIds) throws ClientException {
        DeleteStreamRequest deleteStreamRequest = new DeleteStreamRequest();
        deleteStreamRequest.setVideoId(videoId);
        deleteStreamRequest.setJobIds(jobIds);
        return VOD_CLIENT.getAcsResponse(deleteStreamRequest);
    }

    /**
     * 【彻底删除，物理删除!】删除完整视频
     * 删除视频时会删除源文件，包括视频源文件、转码后的流文件、封面截图等。但CDN缓存不会同步刷新，如果业务需要，可通过点播控制台的刷新功能，来清除CDN节点上的垃圾数据
     * @param videoIds 音视频id 多个ID之间使用半角逗号（,）分隔，最多支持20个视频ID。
     * @return DeleteMezzanineResponse 删除完整视频响应数据
     * @throws ClientException 连接异常
     */
    public static DeleteVideoResponse deleteMezzanine(String videoIds) throws ClientException {
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        deleteVideoRequest.setVideoIds(videoIds);
        return VOD_CLIENT.getAcsResponse(deleteVideoRequest);
    }

    /**
     * 获取视频播放授权
     *
     * @param videoId 视频id
     * @return 获取视频播放授权响应
     * 2023/09/28
     */

    public static GetVideoPlayAuthResponse getVideoPlayAuth(String videoId) throws ClientException {
        GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest();
        getVideoPlayAuthRequest.setVideoId(videoId);
        getVideoPlayAuthRequest.setAuthInfoTimeout(3000L);
        return VOD_CLIENT.getAcsResponse(getVideoPlayAuthRequest);
    }
}
